$(function(){

    $("#contentfade").fadeOut(300);
    $("#section0 span").animate({height:250+'px'},5000);
    $("header").hide();

    $('#news-content').slimScroll({
        height: '100%'
    });

    $('#fullpage').fullpage({
        anchors: ['home', 'store', 'news', 'media', 'contact'],
        menu:'#menu',
        navigation: false,
        scrollOverflow: true,
        normalScrollElements: ".overlay",
        afterRender: function(){
            checkNews();
        },
        afterLoad: function(anchorLink, index){
            $("header").fadeIn(500);
            $('.intro').fadeIn(300);
            $('.section.active').find('h2').animate({height:220},1000);

            if(index == 1){ // home
                $('.overlay').fadeOut();
                $('#overlay-shade').fadeOut();

                $("header").fadeOut();
                $("header, .fullPage-slidesNav , .intro").fadeOut();
            }

            if($(window).width() > 800){
                if(index == 2){ // store
                    var i=0;
                    var lis =  $(".location a")
                    var interval = setInterval(function() {
                        lis.slice(i,i+1).animate({opacity:1});
                        i += 1;
                    }, 100);
                }
                if(index == 3){ // news
                    var i=0;
                    var lis =  $("ul.list li")
                    var interval = setInterval(function() {
                        lis.slice(i,i+1).animate({ marginLeft:"18%", opacity:1},1000);
                        i += 1;
                    }, 300);
                    $(this).find('.pagination').delay(2800).animate({opacity:1},800);
                }
                if(index == 4){ // media
                    $(this).find('#m-slides').delay(800).animate({left:"10%",opacity:1},800);
                }
                if(index == 5){ // contact
                    $(this).find('table').animate({marginLeft:"180px",opacity:1},800);
                }
            }
        },
        onLeave: function(index, direction){
            $('.intro').fadeOut(300);
        }

    }); // fullpage

    
    $('.overlay-close, #overlay-shade').live('click', function(e) {
        closeOverlay();
        if ($(this).attr('href') == '#') e.preventDefault();
    });

	
    // Usage
    $('.list a').click(function(e) {
       openOverlay('.news');
       e.preventDefault();
    });

    $('.location > a').click(function(e) {
       openOverlay('.location-menu');
       e.preventDefault();
    });

    if($(window).width() < 800){
        var windowW = $(window).width();
        shareWidth = $(".share").width();
        realCenter = (windowW - shareWidth)/2;
        $(".overlay .share").css({left:realCenter-10});
    }

	//news slides & media slides
	if($(window).width() > 800){
		$('.media a').mouseenter(function(){
			$(this).find('div').stop().animate({
				top:0,
				opacity:0.7
				},'slow');
			$(this).find('img').animate({
				width : 100 + '%'
				},'slow');
	
		});
		$('.media a').mouseleave(function(){
			$(this).find('div').stop().animate({
				top:-200,
				opacity:0
				},'slow');
			$(this).find('img').animate({
				width : 110 + '%',
				bottom: 0
				},'fast');
		});
	}

    //調整瀏覽器
    resize_fn();

    $(window).resize(resize_fn); //調整瀏覽器則再執行
});

function openOverlay(olEl) {
    var $oLay = $(olEl);

    if ($('#overlay-shade').length == 0) {
        $('body').prepend('<div id="overlay-shade"></div>');
        $('.overlay').prepend('<a class="overlay-close">回主頁</a> ');
    }
   

    $('#overlay-shade').fadeTo(300, 0.8, function() {
        var props = {
            oLayWidth       : $oLay.width(),
            scrTop          : $(window).scrollTop(),
            viewPortWidth   : $(window).width()
        };
        var leftPos = (props.viewPortWidth - props.oLayWidth) / 2;

        $("header, .fullPage-slidesNav ,h2, .intro").hide();
        $oLay.find(".overlay-close").fadeIn();
        $oLay
            .css({
                display : 'block',
                opacity : 0,
                top : '-=300',
                left : leftPos+'px'
            })
            .animate({
                top : props.scrTop,
                opacity : 1
            }, 600);

        if($(window).width() > 800){
            $(".overlay .share").animate({left:3+'%',opacity:1},'slow');
        }
        if($(window).width() < 800){
            $(".overlay .share").animate({bottom:10+'%',opacity:1},'slow');
        }
    });

}

function closeOverlay() {

    if($(window).width() > 800){
        $(".overlay .share").animate({left:-3+'%',opacity:0},'slow');
    }
    if($(window).width() < 800){
        $(".overlay .share").animate({bottom:-10+'%',opacity:0},'slow');
    }
    $('.overlay').animate({
        top : '-=300',
        opacity : 0
    }, 400, function() {
        $('#overlay-shade').fadeOut(300);
        $(this).css('display','none');
        $("header, .fullPage-slidesNav , .intro").fadeIn(300);

        if($(window).width() >= 800){
            $('h2').fadeIn(300);
        }
    });

}

function resize_fn()
{
    var $mobilePos = ($(window).width() - $('.mlogo img').width())/2;
    var $logoPos = ($(window).width() - $('#section0 span').width())/2;
    var $arrowPos = ($(window).width() - $('.index-down').width())/2;
    var $thePos = ($(window).width() - $('header ul').width())/2;
    var $TitlePos = ($(window).width() - $('.intro').width())/2;

    $("#section0 span").css('left',$logoPos+'px');
    $(".index-down").css('left',$arrowPos+'px');
    $(".index-down").css('left',$arrowPos+'px');
    $(".issuu").css({width:( $(window).width() - 300 ) });

    if($(window).width() <= 800){
        $('.issuu').css({width:'100%'});
        $('.issuuembed').css({width:400,height:200});
    }

}